package com.mysiteforme.admin.controller.admin;

import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityElectrovalence;
import com.mysiteforme.admin.service.ElectricityElectrovalenceService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电价管理  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-27
 */
@Controller
@RequestMapping("/admin/electricityElectrovalence")
public class ElectricityElectrovalenceController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityElectrovalenceController.class);

    @Autowired
    private ElectricityElectrovalenceService electricityElectrovalenceService;

    @GetMapping("list")
    @SysLog("跳转电价管理列表")
    public String list() {
        return "/admin/electricityElectrovalence/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求电价管理列表数据")
    public LayerData<ElectricityElectrovalence> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                                     ServletRequest request) {
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityElectrovalence> layerData = new LayerData<>();
        EntityWrapper<ElectricityElectrovalence> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", false);
        if (!map.isEmpty()) {
        }
        Page<ElectricityElectrovalence> pageData = electricityElectrovalenceService.selectPage(new Page<>(page, limit), wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增电价管理页面")
    public String add() {
        return "/admin/electricityElectrovalence/add";
    }

    @PostMapping("add")
    @SysLog("保存新增电价管理数据")
    @ResponseBody
    public RestResponse add(ElectricityElectrovalence electricityElectrovalence) {
        //判断是否存在该等级电价 存在不添加
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("voltage_classes", electricityElectrovalence.getVoltageClasses());
        List<ElectricityElectrovalence> list = electricityElectrovalenceService.selectList(entityWrapper);
        if (list != null && list.size()>0 ){
            return RestResponse.failure("重复添加同等级电价，请验证");
        }
        electricityElectrovalenceService.insert(electricityElectrovalence);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑电价管理页面")
    public String edit(Long id, Model model) {
        ElectricityElectrovalence electricityElectrovalence = electricityElectrovalenceService.selectById(id);
        model.addAttribute("electricityElectrovalence", electricityElectrovalence);
        return "/admin/electricityElectrovalence/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑电价管理数据")
    public RestResponse edit(ElectricityElectrovalence electricityElectrovalence) {
        //判断是否存在该等级电价 存在不修改
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.ne("id",electricityElectrovalence.getId());
        entityWrapper.eq("voltage_classes", electricityElectrovalence.getVoltageClasses());
        List<ElectricityElectrovalence> list = electricityElectrovalenceService.selectList(entityWrapper);
        if (list != null && list.size()>0 ){
            return RestResponse.failure("等级电价已存在，请验证");
        }
        if (null == electricityElectrovalence.getId() || 0 == electricityElectrovalence.getId()) {
            return RestResponse.failure("ID不能为空");
        }
        Boolean b = electricityElectrovalenceService.updateById(electricityElectrovalence);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除电价管理数据")
    public RestResponse delete(@RequestParam(value = "id", required = false) Long id) {
        if (null == id || 0 == id) {
            return RestResponse.failure("ID不能为空");
        }
        ElectricityElectrovalence electricityElectrovalence = electricityElectrovalenceService.selectById(id);
        electricityElectrovalence.setDelFlag(true);
        electricityElectrovalenceService.updateById(electricityElectrovalence);
        return RestResponse.success();
    }

}
